package com.ZengQi.event.dao.impl;

import com.ZengQi.event.dao.TopicDao;
import com.ZengQi.event.entity.Finish;
import com.ZengQi.event.entity.Topic;
import com.ZengQi.event.util.SearchUtils;
import com.ZengQi.event.util.UpdateUtils;

import java.util.List;

public class TopicDaoImpl implements TopicDao {
    @Override
    public boolean insertTopic(Topic topic) {
        String sql="insert into topic values(null,?,?,?,?)";
        boolean update = UpdateUtils.update(sql, topic.getTopicName(), topic.getTopicContent(), topic.getTopicAnswer(), topic.getTopicPublisher());
        return update;
    }

    @Override
    public List<Topic> listTopics(Topic conditionTopic) {
        String sql="select topic_id topicId,topic_name topicName,topic_content topicContent,topic_answer topicAnswer,topic_publisher topicPublisher from topic where topic_name like ? ";
        List<Topic> topics = SearchUtils.searchForList(Topic.class, sql, conditionTopic.getTopicName());
        if(topics.size()==0){
            return null;
        }
        return topics;
    }

    @Override
    public List<Topic> listFinshTopics(Finish conditionFinish) {
        String sql="select finish.topic_name topicName,topic_publisher topicPublisher,topic_content topicContent,topic_answer topicAnswer from finish inner join topic on finish.topic_name=topic.topic_name where finisher =?";
        List<Topic> topics = SearchUtils.searchForList(Topic.class, sql, conditionFinish.getFinisher());
        return topics;
    }

    @Override
    public boolean deleteTopic(Topic conditionTopic) {
        String sql="delete from topic where topic_name=?";
        boolean update = UpdateUtils.update(sql, conditionTopic.getTopicName());

        return update;
    }

    @Override
    public boolean updateTopicByPublisher(Topic topic, String publisher) {
        publisher=topic.getTopicPublisher();
        String sql="update topic set topic_content=? where topic_name=? and topic_publisher=?";
        boolean update = UpdateUtils.update(sql, topic.getTopicContent(), topic.getTopicName(), publisher);


        return update;
    }
}
